package ru.volgogradetzzz.math 
{
	
	/**
	 * @author Volgogradetzzz
	 */
	public class VMatrix2x2 
	{
		//----------------------------------------------
		// Public fields
		//----------------------------------------------
		
		public var m11:Number, m12:Number;
		public var m21:Number, m22:Number;
		
		//----------------------------------------------
		// Constructor
		//----------------------------------------------
		
		public function VMatrix2x2(	m11:Number = 1, m12:Number = 0,
									m21:Number = 0, m22:Number = 1) 
		{
			this.m11 = m11; this.m12 = m12;
			this.m21 = m21; this.m22 = m22;
		}
		
		//----------------------------------------------
		// Public methods
		//----------------------------------------------
		
		public function prepareForRotation(ang:Number):void
		{
			var sin:Number = Math.sin(ang);
			var cos:Number = Math.cos(ang);
			
			m11 = cos; m12 = -sin;
			m21 = sin; m22 = cos;
		}
		
		public function multiplyByVector(v:VVector2):VVector2
		{
			var newVec:VVector2 = new VVector2();
			newVec.x = m11 * v.x + m12 * v.y;
			newVec.y = m21 * v.x + m22 * v.y;
			
			return newVec;
		}
		
		public function multiplyByMatrix(m:VMatrix2x2):void
		{
			var temp11:Number = m11 * m.m11 + m12 * m.m21;
			var temp12:Number = m11 * m.m12 + m12 * m.m22;
			var temp21:Number = m21 * m.m11 + m22 * m.m21;
			var temp22:Number = m21 * m.m12 + m22 * m.m22;
			
			m11 = temp11; m12 = temp12;
			m21 = temp21; m22 = temp22;
		}
		
		public function multiplyByMatrixReturn(m:VMatrix2x2):VMatrix2x2
		{
			var newM:VMatrix2x2 = new VMatrix2x2();
			newM.m11 = m11 * m.m11 + m12 * m.m21;
			newM.m12 = m11 * m.m12 + m12 * m.m22;
			newM.m21 = m21 * m.m11 + m22 * m.m21;
			newM.m22 = m21 * m.m12 + m22 * m.m22;
			
			return newM;
		}
		
		public function transpose():VMatrix2x2
		{
			return new VMatrix2x2(	m11, m21,
									m12, m22);
		}
		
		public function clone():VMatrix2x2
		{
			return new VMatrix2x2(	m11, m12,
									m21, m22);
		}
		
		public function toString():String
		{
			return 	'[' + m11 + ', ' + m12 + ',\n' +
					' ' + m21 + ', ' + m22 + ']';
		}
	}
}